const express = require('express');
const router = express.Router();
const BillController = require('../controllers/BillController');
const authenticate = require('../middleware/auth');

/**
 * @swagger
 * tags:
 *   name: Bills
 *   description: 账单管理
 */

// All routes require authentication
router.use(authenticate);

/**
 * @swagger
 * /api/bills:
 *   post:
 *     summary: 创建新账单
 *     tags: [Bills]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - amount
 *               - type
 *               - bill_date
 *             properties:
 *               category_id:
 *                 type: integer
 *                 description: 分类ID（收入/支出类型必需）
 *               title:
 *                 type: string
 *                 description: 账单标题
 *               amount:
 *                 type: number
 *                 format: float
 *                 description: 金额
 *               type:
 *                 type: string
 *                 enum: [income, expense, transfer]
 *                 description: 账单类型
 *               bill_date:
 *                 type: string
 *                 format: date
 *                 description: 账单日期 (YYYY-MM-DD)
 *               description:
 *                 type: string
 *                 description: 描述
 *               payment_method:
 *                 type: string
 *                 description: 支付方式
 *               account_id:
 *                 type: integer
 *                 description: 账户ID（转账类型为源账户）
 *               transfer_to_account_id:
 *                 type: integer
 *                 description: 转账目标账户ID（转账类型必需）
 *     responses:
 *       201:
 *         description: 账单创建成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.post('/', BillController.createBill);

/**
 * @swagger
 * /api/bills:
 *   get:
 *     summary: 获取用户账单列表
 *     tags: [Bills]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *         description: 每页数量
 *       - in: query
 *         name: start_date
 *         schema:
 *           type: string
 *           format: date
 *         description: 开始日期 (YYYY-MM-DD)
 *       - in: query
 *         name: end_date
 *         schema:
 *           type: string
 *           format: date
 *         description: 结束日期 (YYYY-MM-DD)
 *       - in: query
 *         name: category_id
 *         schema:
 *           type: integer
 *         description: 分类ID
 *       - in: query
 *         name: account_id
 *         schema:
 *           type: integer
 *         description: 账户ID
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [income, expense, transfer]
 *         description: 账单类型
 *       - in: query
 *         name: search
 *         schema:
 *           type: string
 *         description: 搜索关键词
 *     responses:
 *       200:
 *         description: 成功获取账单列表
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/', BillController.getBills);

/**
 * @swagger
 * /api/bills/stats:
 *   get:
 *     summary: 获取账单统计信息
 *     tags: [Bills]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: start_date
 *         schema:
 *           type: string
 *           format: date
 *         description: 开始日期 (YYYY-MM-DD)
 *       - in: query
 *         name: end_date
 *         schema:
 *           type: string
 *           format: date
 *         description: 结束日期 (YYYY-MM-DD)
 *       - in: query
 *         name: account_id
 *         schema:
 *           type: integer
 *         description: 账户ID
 *     responses:
 *       200:
 *         description: 成功获取账单统计信息
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/stats', BillController.getBillStats);

/**
 * @swagger
 * /api/bills/{id}:
 *   get:
 *     summary: 获取特定账单详情
 *     tags: [Bills]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 账单ID
 *     responses:
 *       200:
 *         description: 成功获取账单详情
 *       401:
 *         description: 未授权
 *       404:
 *         description: 账单未找到
 *       500:
 *         description: 服务器内部错误
 */
router.get('/:id', BillController.getBillById);

/**
 * @swagger
 * /api/bills/{id}:
 *   put:
 *     summary: 更新账单
 *     tags: [Bills]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 账单ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               category_id:
 *                 type: integer
 *                 description: 分类ID（收入/支出类型）
 *               title:
 *                 type: string
 *                 description: 账单标题
 *               amount:
 *                 type: number
 *                 format: float
 *                 description: 金额
 *               type:
 *                 type: string
 *                 enum: [income, expense, transfer]
 *                 description: 账单类型
 *               bill_date:
 *                 type: string
 *                 format: date
 *                 description: 账单日期 (YYYY-MM-DD)
 *               description:
 *                 type: string
 *                 description: 描述
 *               payment_method:
 *                 type: string
 *                 description: 支付方式
 *               account_id:
 *                 type: integer
 *                 description: 账户ID（转账类型为源账户）
 *               transfer_to_account_id:
 *                 type: integer
 *                 description: 转账目标账户ID（转账类型必需）
 *     responses:
 *       200:
 *         description: 账单更新成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       404:
 *         description: 账单未找到
 *       500:
 *         description: 服务器内部错误
 */
router.put('/:id', BillController.updateBill);

/**
 * @swagger
 * /api/bills/{id}:
 *   delete:
 *     summary: 删除账单
 *     tags: [Bills]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 账单ID
 *     responses:
 *       200:
 *         description: 账单删除成功
 *       401:
 *         description: 未授权
 *       404:
 *         description: 账单未找到
 *       500:
 *         description: 服务器内部错误
 */
router.delete('/:id', BillController.deleteBill);

module.exports = router;